package com.intel.factorytest.activity;

import java.util.Timer;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import com.intel.factorytest.R;
import android.view.KeyEvent;

public class DisplayStatusDisplayTest extends BaseActivity implements GestureDetector.OnGestureListener {
    
    //static 
    public static final String TAG = "DisplayStatusDisplayTest";
    public static final int FRESH = 1;
    public static final int CHANGE = 2;
    public static final int ADD = 20;
    //
    private int mWidth = 0;
    private int mHeight = 0;
    private Timer mTimer;
    private FihDoodleView mDoodleView;
    private boolean mNoChanged = true;
    private GestureDetector mGestureDetector;
    private Button mSuessbtn;
    private Button mFailebtn;
    private Button mRepeatbtn;
    private Button mNextbtn;//add by wangsm
    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==FRESH){
                if (mDoodleView.getShowDone()) {
                    mDoodleView.invalidate();
                }else {
                    //mDoodleView.invalidate();
                    mDoodleView.setBackground(getResources().getDrawable(R.drawable.blackdisplay));
                    mSuessbtn.setVisibility(View.VISIBLE);
                    mFailebtn.setVisibility(View.VISIBLE);
                    mItemTestFinish = true;//add by wangsm
                    //mRepeatbtn.setVisibility(View.VISIBLE);
                    //mNextbtn.setVisibility(View.GONE);//add by wangsm
                         //finish();
                }
            }
/*
            if(mNoChanged&&mDoodleView.getShowAnimation()){
                mNoChanged = false;
                mTimer.schedule(new TimerTask(){
                    
                    @Override
                    public void run() {
                        mHandler.sendEmptyMessage(FRESH);
                    }
                    
                }, 100, 100);
            }
            
            super.handleMessage(msg);
        */
        }
        
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mGestureDetector = new GestureDetector(this);
        setContentView(R.layout.display);
        initView();//add by wangsm
        initActivity();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mTimer = new Timer();
        /*mTimer.schedule(new TimerTask(){
            
            @Override
            public void run() {
                mHandler.sendEmptyMessage(FRESH);
            }
            
        }, 1000);//, 1000);*/
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
    @Override
    protected void onDestroy() {
        if(mTimer!=null){
            mTimer.cancel();
        }
        super.onDestroy();
    }
    private void initActivity() {
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        mWidth  = dm.widthPixels;
        mHeight = dm.heightPixels;
        Log.v(TAG,"mWidth"+mWidth+"mHeight"+mHeight+" dm.density = " + dm.density+ " dm.densityDpi = " + dm.densityDpi + "  dm.xdpi= "+ dm.xdpi + " dm.ydpi = " +dm.ydpi);
        mDoodleView = (FihDoodleView)findViewById(R.id.displayview);
        mSuessbtn = (Button)findViewById(R.id.success_button);
        mFailebtn = (Button)findViewById(R.id.fail_button);
        mRepeatbtn = (Button) findViewById(R.id.repeat_button);
        mSuessbtn.setVisibility(View.GONE);
        mFailebtn.setVisibility(View.GONE);
        //mNextbtn = (Button) findViewById(R.id.next_button);
        /*mNextbtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mHandler.sendEmptyMessage(FRESH);
            }
        });*/
        mSuessbtn.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                is_pass = true;
//                setTestResult();
                finish();
            }
        });
        
        mFailebtn.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                is_pass = false;
//                setTestResult();
                finish();
            }
        });
        
        mRepeatbtn.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                DisplayStatusDisplayTest.this.recreate();
//                mDoodleView.mIndex = 0;
//                mDoodleView.setDrawAll(true);
//                mSuessbtn.setVisibility(View.GONE);
//                mFailebtn.setVisibility(View.GONE);
//                mRepeatbtn.setVisibility(View.GONE);
//                mDoodleView.invalidate();
            }
        });
        
        mDoodleView.setWidthHeight(mWidth, mHeight);
    }

    @Override  
    public boolean onTouchEvent(MotionEvent event) {  
            return mGestureDetector.onTouchEvent(event);  
    }  

    @Override
    public void finish() {
        tc.setTestResult(is_pass);
        super.finish();
    }

    @Override
    public boolean onDown(MotionEvent e) {
        Log.i("meilianggang", "=====ondown===== action = " + e.getAction() + " x = " + e.getX() + " y = " + e.getY());
        mHandler.sendEmptyMessage(FRESH);
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        Log.i("meilianggang", "=====onFling===== action1 = " + e1.getAction() + " x = " + e1.getX() + " y = " + e1.getY() + "  action2 = " + e2.getAction() + " x = " + e2.getX() + " y = " + e2.getY() + "  velocityX =  " + velocityX + "  velocityY = " + velocityY);
        


        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override    
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        //Log.i("meilianggang", "=====onScroll===== action1 = " + e1.getAction() + " x = " + e1.getX() + " y = " + e1.getY() + "  action2 = " + e2.getAction() + " x = " + e2.getX() + " y = " + e2.getY() + "  distanceX =  " + distanceX + "  distanceY = " + distanceY);
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return true;
    }
    //add by wangsm
    @Override
    public boolean dispatchKeyEvent(KeyEvent event){
         Log.d(TAG, " wangsm display dispatchKeyEvent");
         int keyCode = event.getKeyCode();
         int keyAction = event.getAction();
         if(keyCode == KeyEvent.KEYCODE_CALL &&  (keyAction == KeyEvent.ACTION_UP)){
             mHandler.sendEmptyMessage(FRESH);
             return true;
         }else if(keyCode == KeyEvent.KEYCODE_CALL){
             return true;
         }

         return super.dispatchKeyEvent(event);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
         Log.d(TAG, " wangsm display onKeyDown");
         if(keyCode != KeyEvent.KEYCODE_BACK && keyCode != KeyEvent.KEYCODE_MENU){
               mHandler.sendEmptyMessage(FRESH);
               return false;
             //finish();
         }/*else{
             mHandler.sendEmptyMessage(FRESH);
             return false;
         }*/

        return super.onKeyDown(keyCode, event);
    }

//add by wangsm for left soft key.
    protected void initView() {
         super.initView();
     }

}
